require 'rake'
require 'rake/packagetask'
require 'erb'
require 'find'

SLIDESHOW_ROOT = File.expand_path(File.dirname(__FILE__))
SLIDESHOW_SRC_PATH = File.join(SLIDESHOW_ROOT, 'src')
SLIDESHOW_DIST_PATH = File.join(SLIDESHOW_ROOT, 'dist')
SLIDESHOW_LIB_PATH = File.join(SLIDESHOW_ROOT, 'lib')
SLIDESHOW_PKG_PATH  = File.join(SLIDESHOW_ROOT, 'pkg')
SLIDESHOW_EXAMPLE_JS_PATH = File.join(SLIDESHOW_ROOT, 'examples/js')
SLIDESHOW_EXAMPLE_CSS_PATH = File.join(SLIDESHOW_ROOT, 'examples/css')
SLIDESHOW_VERSION = "0.4pre-release"

task :default => [:clean_package_source, :clean_examples, :clean_dists, :clean_packages]

task :examples => [:dist]

task :package => [:examples]

task :examples do
  # Move all of the js and css assets into place
  File.copy(File.join(SLIDESHOW_DIST_PATH, 'slideshow.js'), SLIDESHOW_EXAMPLE_JS_PATH)
  File.copy(File.join(SLIDESHOW_LIB_PATH, 'prototype/prototype.js'), SLIDESHOW_EXAMPLE_JS_PATH)
  
  Find.find(File.join(SLIDESHOW_LIB_PATH, 'scriptaculous/')) do |path|
    File.copy(path, SLIDESHOW_EXAMPLE_JS_PATH) unless FileTest.directory?(path) || path.include?('.svn')
  end
  
  File.copy(File.join(SLIDESHOW_DIST_PATH, 'slideshow.css'), SLIDESHOW_EXAMPLE_CSS_PATH)
  
  # Compile the correct example templates
  @prototype = 'js'
  @scriptaculous = 'js'
  @js = 'js'
  @css = 'css'
  
  Find.find(File.join(SLIDESHOW_LIB_PATH, 'examples')) do |path|
    unless FileTest.directory?(path) || path.include?('.svn') || path.include?('/._') || path.include?('.DS_Store')
      path_parts = path.split('/')
      filename = path_parts[path_parts.length-1].split('.')[0]
      
      File.open(File.join(SLIDESHOW_ROOT, 'examples/'+filename+'.html'), 'w+') do |example|
        example << ERB.new(File.read(path)).result(binding)
      end
    end
  end

end

task :dist do
  File.open(File.join(SLIDESHOW_DIST_PATH, 'slideshow.js'), 'w+') do |dist|
    dist << ERB.new(File.read(File.join(SLIDESHOW_SRC_PATH, 'slideshow.js')), nil).result(binding)
  end
  
  File.open(File.join(SLIDESHOW_DIST_PATH, 'slideshow.css'), 'w+') do |dist|
    dist << ERB.new(File.read(File.join(SLIDESHOW_SRC_PATH, 'slideshow.css')), nil).result(binding)
  end
end



Rake::PackageTask.new('slideshow.js', SLIDESHOW_VERSION) do |package|
  package.need_zip = true
  package.package_dir = SLIDESHOW_PKG_PATH
  
  files = []
  Find.find(SLIDESHOW_DIST_PATH) do |path|
    files << path.split(SLIDESHOW_ROOT+'/')[1] unless FileTest.directory?(path) || path.include?('.svn') || path.include?('/._') || path.include?('.DS_Store')
  end
  
  Find.find(File.join(SLIDESHOW_ROOT, 'examples')) do |path|
    files << path.split(SLIDESHOW_ROOT+'/')[1] unless FileTest.directory?(path) || path.include?('.svn') || path.include?('/._') || path.include?('.DS_Store')
  end
  
  package.package_files.include(files.flatten)
end

task :clean_package_source do
  rm_rf File.join(SLIDESHOW_PKG_PATH, "slideshow.js-#{SLIDESHOW_VERSION}")
end

task :clean_examples do 
  Find.find(SLIDESHOW_EXAMPLE_JS_PATH) do |path|
    rm_rf path unless FileTest.directory?(path) || path.include?('.svn')
  end
  
  rm_rf File.join(SLIDESHOW_EXAMPLE_CSS_PATH, 'slideshow.css')
  
  # Compile the correct example templates
  @prototype = '../lib/prototype'
  @scriptaculous = '../lib/scriptaculous'
  @js = '../src'
  @css = '../src'
  
  Find.find(File.join(SLIDESHOW_LIB_PATH, 'examples')) do |path|
    unless FileTest.directory?(path) || path.include?('.svn') || path.include?('/._') || path.include?('.DS_Store')
      path_parts = path.split('/')
      filename = path_parts[path_parts.length-1].split('.')[0]
      
      File.open(File.join(SLIDESHOW_ROOT, 'examples/'+filename+'.html'), 'w+') do |example|
        example << ERB.new(File.read(path)).result(binding)
      end
    end
  end
end

task :clean_dists do
  Find.find(SLIDESHOW_DIST_PATH) do |path|
    rm_rf path unless FileTest.directory?(path) || path.include?('.svn')
  end
end

task :clean_packages do 
  Find.find(SLIDESHOW_PKG_PATH) do |path|
    rm_rf path unless FileTest.directory?(path) || path.include?('.svn')
  end
end